#coding=utf-8
import sys
import os
import types
import pdb
from python.lib.xlrd import xlrd
import json
def open_excel(file):
    try:
        data = xlrd.open_workbook(file,"utf-8")
        return data
    except Exception as e:
        print (e)

#解析目录文件
#@param file 目录文件名
def open_contents(file, outputClientDir):
    data = open_excel(file)
    table = data.sheet_by_index(0)
    nrows = table.nrows;
    for index in range(1,nrows):
        row = table.row_values(index)
        #要打表的对象文件
        input = '../' + row[0]
        if not os.path.exists(input):
            print(row[0] + " 不存在!!")
            continue
        else:
            print('输出表格:' + row[0])
        #pdb.set_trace()
        #索引行
        indexRow = converToInt(row[1])
        #索引列
        indexCol = converToInt(row[2])
        #开始行
        startRow = converToInt(row[3])
        #结束行
        endRow = converToInt(row[4])
        #开始列
        startCol = converToInt(row[5])
        #结束列
        endCol = converToInt(row[6])
        #pdb.set_trace()
        #忽略行
        ignoreRow = converToStr(row[7]).split("|")
        for x in range(len(ignoreRow)):
            if ignoreRow[x] !='':
                ignoreRow[x] = converToInt(ignoreRow[x])
        #忽略列 
        ignoreCol = converToStr(row[8]).split("|")
        for x in range(len(ignoreCol)):
            if ignoreCol[x] != '':
                ignoreCol[x] = converToInt(ignoreCol[x])
        #输出目录
        outputFileDir = row[9]
        file = open(outputClientDir +'/' + outputFileDir,'w',encoding='UTF-8')
        #当前子表格
        childTableData = open_excel(input)
        if childTableData == None:
            print(input + '文件打开失败!')
            continue
        childTableData = childTableData.sheet_by_index(0)
        #当期表格有多少行
        childTableRows = childTableData.nrows
        #当前表格有多少列
        childTableCols = childTableData.ncols
        if endCol == 0 :
            endCol = childTableCols
        if endRow == 0:
            endRow = childTableRows
        #判断索引行列有效性
        if IsOutOfRange(0,childTableRows,indexRow):
            print(input + '索引行超出有效范围!')
            continue
        if IsOutOfRange(0,childTableCols,indexCol):
            print(input + '索引列超出有效范围!')
            continue
        if IsOutOfRange(indexCol,childTableCols,startCol):
            print(input + '开始行超出有效范围!')
            continue
        if IsOutOfRange(indexCol,childTableCols,endCol):
            print(input + '结束行超出有效范围!')
            continue
        if IsOutOfRange(indexRow,childTableRows,startRow):
            print(input + '开始列超出有效范围!')
            continue
        if IsOutOfRange(indexRow,childTableRows,endRow):
            print(input + '结束列超出有效范围!')
            continue
        #获取key值 
        #行
        childRows = childTableData.row_values(indexRow - 1)
        #列
        childCols = childTableData.col_values(indexCol - 1)
        arrayDatas = {}
        for row in range(startRow,endRow+1):
            #忽略行
            if row in ignoreRow:
                continue
            arrayData = {}
            for col in range(startCol,endCol+1):
                #忽略列
                if col in ignoreCol:
                    continue
                currentRows = childTableData.col_values(col-1)
                if currentRows[row - 1] =="":
                    continue
                #kv结构
                arrayData[ childRows[col - 1] ] = converIntToInt(currentRows[row - 1])

            if len(arrayData) > 0 :
                arrayDatas[ converIntToInt(childCols[row-1]) ] = arrayData
        #写入json
        file.write(json.dumps(arrayDatas,ensure_ascii=False,indent=2))
        file.close()

#判断是否在这个范围内
def IsOutOfRange(minValue,maxValue,value):
    if value >= minValue and value <= maxValue:
        return False
    return True

def converToInt(value):
    temp = value
    if value == None :
        temp = 0
    if type(value) == float:
        temp = int(value)
    if type(value) == str:
        if value == "":
            temp = 0
        else:
            #int 不能转换带小数点的值 需要先转换到float
            temp = int(float(value))
    return temp

def converToStr(value):
    temp = value
    if value == None :
        temp = ''
    if type(value) == float:
        temp = str(value)
    return temp

def converIntToInt(value):
    if type(value) == float and value == int(value):
        value = int(value)
    return value

def main(argv):
    fileName = argv[1]
    outputClientDir = argv[2]
    outputClientDir = outputClientDir.strip()
    open_contents(fileName,outputClientDir)

if __name__ == '__main__':
    main(sys.argv)
